package ru.mcfr.oxygen.framework.extensions;

import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.highlights.AuthorPersistentHighlight.PersistentHighlightType;
import ro.sync.exml.view.graphics.Color;
import ru.mcfr.oxygen.framework.filters.MOFDocumentFilter;
import ru.mcfr.oxygen.framework.listeners.MOFAuthorMouseListener;

import java.util.HashMap;
import java.util.Map;

public class MOFAuthorExtensionStateListener implements AuthorExtensionStateListener {
    private String customKey = "sdf.custom.option.key";

    private AuthorCaretListener sdfCaretListener;

    private AuthorMouseListener sdfMouseListener;

    private OptionListener sdfOptionListener;

    private AuthorListener sdfAuthorDocumentListener;

    public static Map<String, Map<PersistentHighlightType, HighlightColors>> authorHighlightColors = null;

    public static class HighlightColors {
        private Color bgColor;
        private Color decorationColor;

        public HighlightColors(Color bgColor, Color decorationColor) {
            super();
            this.bgColor = bgColor;
            this.decorationColor = decorationColor;
        }

        public Color getBgColor() {
            return bgColor;
        }

        public Color getDecorationColor() {
            return decorationColor;
        }
    }

    static {
        authorHighlightColors = new HashMap<String, Map<PersistentHighlightType, HighlightColors>>();

        // Set colors for Author_1
        Map<PersistentHighlightType, HighlightColors> colorsMap = new HashMap<PersistentHighlightType, HighlightColors>();
        // Set colors for Insert Change highlight
        colorsMap.put(PersistentHighlightType.CHANGE_INSERT,
                new HighlightColors(new Color(230, 255, 230), new Color(130, 255, 130)));
        // Set colors for Delete Change highlight
        colorsMap.put(PersistentHighlightType.CHANGE_DELETE,
                new HighlightColors(new Color(255, 255, 230), new Color(255, 255, 130)));
        authorHighlightColors.put("Author_1", colorsMap);

        // Set colors for Author_2
        colorsMap = new HashMap<PersistentHighlightType, HighlightColors>();
        // Set colors for Insert Change highlight
        colorsMap.put(PersistentHighlightType.CHANGE_INSERT,
                new HighlightColors(new Color(255, 255, 230), new Color(255, 255, 130)));
        // Set colors for Delete Change highlight
        colorsMap.put(PersistentHighlightType.CHANGE_DELETE,
                new HighlightColors(new Color(240, 240, 240), new Color(64, 64, 64)));
        authorHighlightColors.put("Author_2", colorsMap);
    }

    public void activated(final AuthorAccess authorAccess) {
//    sdfOptionListener = new OptionListener(customKey) {
//
//      public void optionValueChanged(OptionChangedEvent newValue) {
//        // The custom option changed. 
//      }
//    };
//
//    // Add document filter.
        authorAccess.getDocumentController().setDocumentFilter(new MOFDocumentFilter(authorAccess));


//
//    // Add an option listener.
//    authorAccess.getOptionsStorage().addOptionListener(sdfOptionListener);
//
//    // Add author document listeners.
//    sdfAuthorDocumentListener = new SDFAuthorListener(authorAccess);
//    authorAccess.getDocumentController().addAuthorListener(sdfAuthorDocumentListener);
//
//    // Add mouse listener.
    sdfMouseListener = new MOFAuthorMouseListener(authorAccess);
    authorAccess.getEditorAccess().addAuthorMouseListener(sdfMouseListener);
//
//    // Add caret listener.
//    sdfCaretListener = new SDFAuthorCaretListener(authorAccess);
//    //authorAccess.getEditorAccess().addAuthorCaretListener(sdfCaretListener);
//    
//    // Set highlights tool tip and painter 
//    authorAccess.getEditorAccess().getPersistentHighlighter().setHighlightRenderer(new PersistentHighlightRenderer() {
//      public String getTooltip(AuthorPersistentHighlight highlight) {
//        // Get highlight properties
//        Map<String, String> properties = highlight.getClonedProperties();
//        String highlightID = properties.get(HighlightProperties.ID);
//        String highlightAuthor = properties.get(HighlightProperties.AUTHOR);
//        String highlightComment = properties.get(HighlightProperties.COMMENT);
//        
//        StringBuilder tooltip = new StringBuilder();
//        // Add create date
//        if (highlightID != null) {
//          try {
//            tooltip.append("Id: ").append(highlightID).append("\n");
//          } catch (NumberFormatException e) {
//            // Wrong date
//          }
//        }
//        // Add author
//        if (highlightAuthor != null) {
//          tooltip.append("Author: ").append(highlightAuthor).append("\n");;
//        }
//        // Add comment
//        if (highlightComment != null) {
//          tooltip.append("Comment: ").append(highlightComment);
//        }
//        return tooltip.toString();
//      }
//      
//      public HighlightPainter getHighlightPainter(AuthorPersistentHighlight highlight) {
//        ColorHighlightPainter painter = new ColorHighlightPainter();
//        painter.setBgColor(new Color(0, 0, 255, 20));
//        return painter;
//      }
//    });
//    
//    // Set reviews tool tip and painters
//    authorAccess.getReviewController().setReviewRenderer(new PersistentHighlightRenderer() {
//      /**
//       * @see ro.sync.ecss.extensions.api.highlights.PersistentHighlightRenderer#getTooltip(ro.sync.ecss.extensions.api.highlights.AuthorPersistentHighlight)
//       */
//      public String getTooltip(AuthorPersistentHighlight highlight) {
//        String tooltip = "Review Tooltip";
//        
//        PersistentHighlightType type = highlight.getType();
//        if (type == PersistentHighlightType.CHANGE_DELETE) {
//          // Delete highlight
//          tooltip = "Deleted by " + highlight.getClonedProperties().get("author");
//        } else if (type == PersistentHighlightType.CHANGE_INSERT) {
//          // Insert highlight
//          tooltip = "Inserted by " + highlight.getClonedProperties().get("author");
//        } else if (type == PersistentHighlightType.COMMENT) {
//          // Comment highlight
//          // If a null value is returned, the default tooltip text will be used
//          tooltip = null;
//        }
//        return tooltip;
//      }
//      
//      /**
//       * @see ro.sync.ecss.extensions.api.highlights.PersistentHighlightRenderer#getHighlightPainter(ro.sync.ecss.extensions.api.highlights.AuthorPersistentHighlight)
//       */
//      public HighlightPainter getHighlightPainter(AuthorPersistentHighlight highlight) {
//        HighlightPainter reviewPainter = null;
//        Map<String, String> properties = highlight.getClonedProperties();
//        String authorName = properties.get("author");
//        if (authorName != null) {
//          // Get the highlight/colors association for this author name
//          Map<PersistentHighlightType, HighlightColors> highlightColorsMap = authorHighlightColors.get(authorName);
//          if (highlightColorsMap != null) {
//            // Get the associated colors for this type of highlight 
//            HighlightColors highlightColors = highlightColorsMap.get(highlight.getType());
//            if (highlightColors != null) {
//              reviewPainter = new ColorHighlightPainter();
//              // Background color
//              ((ColorHighlightPainter) reviewPainter).setBgColor(highlightColors.getBgColor());
//              // Decoration color
//              ((ColorHighlightPainter) reviewPainter).setColor(highlightColors.getDecorationColor());
//            }
//          }
//        }
//        
//        // If a null value is returned the default highlight painter is used.
//        return reviewPainter;
//      }
//    });
//
//    // Get the list with all track changes highlights from the document 
//    AuthorPersistentHighlight[] changeHighlights = authorAccess.getReviewController().getChangeHighlights();
//    if(changeHighlights.length > 0 && !authorAccess.getReviewController().isTrackingChanges()) {
//      // If the document has track changes highlights, then set track changes mode to ON
//      authorAccess.getReviewController().toggleTrackChanges();
//    }
//    
//    // Add a popup menu customizer. Group Cut, Copy, Paste and Paste as XML actions in Edit menu.
//    authorAccess.getEditorAccess().setPopUpMenuCustomizer(new AuthorPopupMenuCustomizer() {
//      // Customize popup menu
//      public void customizePopUpMenu(Object popUp, final AuthorAccess authorAccess) {
//        JPopupMenu popupMenu = (JPopupMenu) popUp;
//        Component[] components = popupMenu.getComponents();
//        // The new 'Edit' menu
//        JMenu editMenu = new JMenu("Edit");
//        popupMenu.add(editMenu, 0);
//        for (int i = 0; i < components.length; i++) {
//          if (components[i] instanceof JMenuItem) {
//            JMenuItem menuItem = (JMenuItem) components[i];
//            if (menuItem.getAction() != null) {
//              Object name = menuItem.getAction().getValue(Action.NAME);
//              if ("Cut".equals(name) || "Copy".equals(name) || "Paste".equals(name)|| "Paste as XML".equals(name)) {
//                // Remove edit actions
//                popupMenu.remove(menuItem);
//                // Add edit actions to edit menu
//                editMenu.add(menuItem);
//              }
//            }
//          }
//        }
//        
//        final URL selectedUrl;
//        try {
//          final String selectedText = authorAccess.getSelectedText();
//          if (selectedText != null) {
//            selectedUrl = new URL(selectedText);
//            // Open selected url in system application
//            popupMenu.add(new JMenuItem(new AbstractAction("Open in system application") {
//              public void actionPerformed(ActionEvent e) {
//                authorAccess.getWorkspaceAccess().openInExternalApplication(selectedUrl, true);
//              }
//            }), 0);
//          }
//        } catch (MalformedURLException e2) {}
//      }
//    });


        // Other custom initializations...
    }

    public void deactivated(AuthorAccess authorAccess) {

//    // Remove the option listener.
//    authorAccess.getOptionsStorage().removeOptionListener(sdfOptionListener);
//
//    // Remove document listeners.
    authorAccess.getDocumentController().removeAuthorListener(sdfAuthorDocumentListener);
//
//    // Remove mouse listener.
    authorAccess.getEditorAccess().removeAuthorMouseListener(sdfMouseListener);
//
//    // Remove caret listener.
//    authorAccess.getEditorAccess().removeAuthorCaretListener(sdfCaretListener);
//
//    // Other actions...
    }

    public String getDescription() {
        return "Simple Document Framework state listener";
    }
}
